package com.ilibrary.web.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import com.ilibrary.base.MyAjaxAction;
import com.ilibrary.base.MyBaseVO;
import com.ilibrary.constants.MyConstants;
import com.ilibrary.constants.MyMsgConstants;
import com.ilibrary.service.BookService;
import com.ilibrary.utils.MyColumnIndicator;
import com.ilibrary.utils.MyTools;
import com.ilibrary.web.dto.ResultDto;
import com.ilibrary.web.vo.AdminVO;
import com.ilibrary.web.vo.BookVO;

public class DoAddBookAction extends MyAjaxAction {

	private BookService bookService;
	
	public BookService getBookService() {
		return bookService;
	}
	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}

	@Override
	protected JSONObject doExecute(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
		JSONObject jo = new JSONObject();
		String bookName = request.getParameter("bookName");
		String bookAuthor = request.getParameter("bookAuthor");
		String bookPubHouse = request.getParameter("bookPubHouse");
		String bookPubDt = request.getParameter("bookPubDt");
		String bookPrice = request.getParameter("bookPrice");
		String bookCnt = request.getParameter("bookCnt");
		String categoryId = request.getParameter("categoryId");
		
		BookVO bookVO = new BookVO();
		bookVO.setBookId(0);
		bookVO.setCategoryId(categoryId);
		bookVO.setBookName(bookName);
		bookVO.setBookAuthor(bookAuthor);
		bookVO.setBookPrice(Float.valueOf(bookPrice));
		bookVO.setBookStatus(MyColumnIndicator.BOOK_STATUS_IN_LIB);
		bookVO.setBookPubHouse(bookPubHouse);
		bookVO.setBookPubDt(MyTools.formateString2Date(bookPubDt, "yyyy-mm-dd"));
		Date date = new Date();
		bookVO.setCreateDt(date);
		bookVO.setUpdateDt(date);
		
		AdminVO adminVO = (AdminVO) session.getAttribute(MyConstants.ILIBRARY_LOGIN_ADMIN);
		
		bookVO.setCreateUser(adminVO.getAdminId());
		bookVO.setUpdateUser(adminVO.getAdminId());
		
		for (int i = 0; i < Integer.parseInt(bookCnt); i++) {
			ResultDto<MyBaseVO> resultDto = bookService.add(bookVO);
			if(!resultDto.isSuccess()) {
				jo.put("success", false);
				jo.put("msg", resultDto.getMsg());
				return jo;
			}
		}
		jo.put("success", true);
		return jo;
	}

}
